/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.panomedic.gui;

import com.panomedic.Constants;
import com.panomedic.utils.Utils;
import com.panomedic.colors.ColorConstants;
import com.panomedic.kernel.VisualHandler;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;

/**
 *
 * @author Yare
 */
public class OptionsDialog extends JDialog implements ActionListener {

    GridBagConstraints constraints = new GridBagConstraints();
    JPanel contentPane = new JPanel(new GridBagLayout());
    JComboBox methodCombo;
    JLabel lutAccLabel;
    JSpinner lutAccSpin;
    JSpinner blurRadSpin;
    JComboBox csCombo;
    JCheckBox[] compCheck;
    
    JCheckBox saveEnd;
    JCheckBox saveStep;
    JButton saveButton;
    JButton cancelButton;
    private Preferences prefs;

    public OptionsDialog(Frame owner, String title) {
        super(owner, title, true);      // dialog is modal
        prefs = Preferences.userRoot().node(Constants.defNodePath);

        
//        constraints.fill = GridBagConstraints.HORIZONTAL;
//        constraints.insets = new Insets(0, 50, 0, 0);
//        constraints.weightx = 0;
//        constraints.weighty = 0;

//        method combo
        String[] methodStrings = {"Histogram Statistics", "Level Mapping"};
        JLabel methodLabel = new JLabel("Used method");
        methodCombo = new JComboBox(methodStrings);
        methodCombo.setSelectedIndex(prefs.getInt(Constants.METHOD_CHOICE, Constants.METHOD_LUT));
        methodCombo.addActionListener(this);


//        lut accuracy
//
//        lutAccLabel = new JLabel("Lookup Table Accuracy");
//        SpinnerModel lutAccModel = new SpinnerNumberModel(10, //initial value
//                0, //min
//                100, //max
//                10);   //step
//        lutAccSpin = new JSpinner(lutAccModel);
//        if (methodCombo.getSelectedIndex() == Constants.METHOD_LUT) {
//            setLutAcc(true);
//            lutAccSpin.setValue(prefs.getInt(Constants.LUT_ACCURACY, Options.LUT_ACCURACY));
//        } else {
//            setLutAcc(false);
//        }

//          blur radius

//        JLabel blurRadLabel = new JLabel("Blur Radius");
        SpinnerModel blurRadModel = new SpinnerNumberModel(prefs.getInt(Constants.BLUR_RADIUS, 10), //initial value
                0, //min
                200, //max
                1);   //step
        blurRadSpin = new JSpinner(blurRadModel);

//        if (methodCombo.getSelectedIndex() != Options.METHOD_LUT) {
//            blurRadLabel.setEnabled(false);
//            blurRadSpin.setEnabled(false);
//        }        

//        colorspace combo
//        String[] csStrings = new String[ColorConstants.NUM_CS];
//        for (int i = 0; i < csStrings.length; i++) {
//            csStrings[i] = ColorConstants.SPACE_NAME[i];
//        }
//        JLabel csLabel = new JLabel("Color Space");
//        csCombo = new JComboBox(csStrings);
//        csCombo.setSelectedIndex(prefs.getInt(Constants.CS_TYPE, ColorConstants.HSV));
//        csCombo.addActionListener(this);
//
////        used components
//        JLabel compLabel = new JLabel("Save automatically after processing");
//        compCheck = new JCheckBox[3];
//        for (int i = 0; i < ColorConstants.NUM_COMP; i++) {
//            compCheck[i] = new JCheckBox(ColorConstants.getCompName(prefs.getInt(Constants.CS_TYPE, ColorConstants.HSV), i),
//                    Utils.getCompProc(prefs.getInt(Constants.COMP_PROC, 4))[i]);
//
//        }

//        saveButton = new JButton("Save");
//        saveButton.addActionListener(this);
//        cancelButton = new JButton("Cancel");
//        cancelButton.addActionListener(this);


//        methodLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
//        int x, y;
////        constraints.insets.left = 20;
//        constraints.gridwidth = 1;
//        addGB(this, methodLabel, x = 0, y = 0);
//        addGB(this, methodCombo, x = 1, y = 0);
//        addGB(this, lutAccLabel, x = 0, y = 1);
//        addGB(this, lutAccSpin, x = 1, y = 1);
//        addGB(this, blurRadLabel, x = 0, y = 2);
//        addGB(this, blurRadSpin, x = 1, y = 2);
//        addGB(this, csLabel, x = 0, y = 3);
//        addGB(this, csCombo, x = 1, y = 3);

//        //  component row
//        JPanel compRow = new JPanel();
//        compRow.setBorder(BorderFactory.createLineBorder(Color.BLACK));
//        constraints.gridwidth = 1;
//        constraints.weightx = 0.0;
//        addGB(compRow, compLabel, x = 0, y = 3);
//        
//        constraints.weightx = 0.0;
        
        // Save result at the processing end
        constraints.weightx = 0;
        JLabel saveEndLabel = new JLabel("Save result automatically after processing");
//        addGB(this, saveEndLabel, x = 0, y = 3);
        
        constraints.weightx = 0;
        saveEnd = new JCheckBox();
        saveEnd.setSelected(prefs.getInt(Constants.SAVE_END, 0) > 0 ? true : false );
//        addGB(this, saveEnd, x = 3, y = 3);
        
        // Save after every step
        constraints.weightx = 0;
        JLabel saveStepLabel = new JLabel("Save result after every step");
//        addGB(this, saveStepLabel, x = 0, y = 4);
        
        constraints.weightx = 0;
        saveStep = new JCheckBox();
        saveStep.setSelected(prefs.getInt(Constants.SAVE_END, 0) > 1  ? true : false );
//        addGB(this, saveEnd, x = 3, y = 4);
        
        
//        for (int i = 0; i < compCheck.length; i++) {
////            addGB(compRow, compCheck[i], x = i + 1, y = 4);
//        }
        constraints.gridwidth = 1;
//        addGB( this, compRow, x = 0, y = 1);

        //  button row
//        JPanel butPanel = new JPanel(new GridBagLayout());
        
//        addGB(butPanel, saveButton, x = 0, y = 0);
//        addGB(butPanel, cancelButton, x = 1, y = 1);
//        addGB(this,butPanel, x = 0, y = 6);
        
//        for (int i = 0; i < 9; i++) {
//            JPanel jp = new JPanel();
//            jp.setBackground(Color.BLACK);
//            addGB(this,jp,4,i);
//        }
        
//        contentPane.add(new OptionsPanel());

        contentPane.setOpaque(true);
        this.setContentPane(contentPane);

        //Show it.
        setResizable(true);
        this.setLocation(300, 300);
        this.setSize(new Dimension(500, 500));
        this.setLocationRelativeTo(owner);
        this.setVisible(true);
    }

    private void addGB(Container cont, JComponent comp, int x, int y) {
        if ((cont.getLayout() instanceof GridBagLayout) == false) {
            cont.setLayout(new GridBagLayout());
        }
        constraints.gridx = x;
        constraints.gridy = y;
        contentPane.add(comp, constraints);
    }

    private void setLutAcc(boolean value) {
        lutAccSpin.setEnabled(value);
        lutAccLabel.setEnabled(value);
    }

    private void setCompLabels(int csType) {
        for (int i = 0; i < ColorConstants.NUM_COMP; i++) {
            compCheck[i].setText(ColorConstants.getCompName(csType, i));
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == methodCombo) {
//            if (methodCombo.getSelectedIndex() == Constants.METHOD_LUT) {
//                setLutAcc(true);
//            } else {
//                setLutAcc(false);
//            }
        } else if (e.getSource() == csCombo) {
            setCompLabels(csCombo.getSelectedIndex());
        } else if (e.getSource() == saveButton) {

//            JComponent editor = lutAccSpin.getEditor();
//            ((DefaultEditor)editor).getTextField().getValue();
//            lutAccSpin.getModel().

            prefs.putInt(Constants.CS_TYPE, csCombo.getSelectedIndex());
            prefs.putInt(Constants.COMP_PROC, Utils.getCompProc(new boolean[]{
                        compCheck[0].isSelected(),
                        compCheck[1].isSelected(),
                        compCheck[2].isSelected()
                    }));
            prefs.putInt(Constants.METHOD_CHOICE, methodCombo.getSelectedIndex());
            prefs.putInt(Constants.LUT_ACCURACY, (Integer) lutAccSpin.getValue());
            prefs.putInt(Constants.BLUR_RADIUS, (Integer) blurRadSpin.getValue());
            int save = 0;
            if (saveEnd.isSelected()) {
                save++;
                if (saveStep.isSelected()) {
                    save++;
                }
            }                
            prefs.putInt(Constants.SAVE_END, save);
            
            try {
                prefs.flush();
            } catch (BackingStoreException ex) {
                Logger.getLogger(OptionsDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (methodCombo.getSelectedIndex() == Constants.METHOD_LUT) {
                VisualHandler.getInstance().getFrame().getToolBar().setShowLutEnabled(true);
            } else {
                VisualHandler.getInstance().getFrame().getToolBar().setShowLutEnabled(false);
            }
            this.setVisible(false);
        } else if (e.getSource() == cancelButton) {
            this.setVisible(false);

        }
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("Options dialog Frame");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton closeButton = new JButton("Show");
        closeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                OptionsDialog dialog = new OptionsDialog(f, "Options dialog");
            }
        });

        f.add(closeButton);
        f.pack();
        f.setVisible(true);
    }
}
